package com.aidex.biz.service.impl;

import com.aidex.biz.domain.SearchInfo;
import com.aidex.biz.mapper.SearchInfoMapper;
import com.aidex.biz.service.SearchInfoService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品信息Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-03-31
 */
@Service
@Transactional(readOnly = true)
public class SearchInfoServiceImpl extends BaseServiceImpl<SearchInfoMapper, SearchInfo> implements SearchInfoService {
    private static final Logger log = LoggerFactory.getLogger(SearchInfoServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param searchInfo 产品信息
     * @return 产品信息
     */
    @Override
    public SearchInfo get(SearchInfo searchInfo) {
        SearchInfo dto = super.get(searchInfo);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 产品信息id
     * @return 产品信息
     */
    @Override
    public SearchInfo get(String id) {
        SearchInfo dto = super.get(id);
        return dto;
    }


    /**
     * 查询产品信息列表
     *
     * @param searchInfo 产品信息
     * @return 产品信息
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<SearchInfo> findList(SearchInfo searchInfo) {
        return super.findList(searchInfo);
    }

    /**
     * 分页查询产品信息列表
     *
     * @param searchInfo 产品信息
     * @return 产品信息
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<SearchInfo> findPage(SearchInfo searchInfo) {
        return super.findPage(searchInfo);
    }

    /**
     * 保存产品信息
     *
     * @param searchInfo
     * @return 结果
     */
    @Override
    public boolean save(SearchInfo searchInfo) {
        return super.save(searchInfo);
    }


    /**
     * 删除产品信息信息
     *
     * @param searchInfo
     * @return 结果
     */
    @Override
    public boolean remove(SearchInfo searchInfo) {
        return super.remove(searchInfo);
    }

    /**
     * 批量删除产品信息
     *
     * @param ids 需要删除的产品信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSearchInfoByIds(String[] ids) {
        return mapper.deleteSearchInfoByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param searchInfo 产品信息
     * @return 结果
     */
    @Override
    public int findMaxSort(SearchInfo searchInfo) {
        return NumberUtils.nextOrder(mapper.findMaxSort(searchInfo));
    }

    @Transactional(readOnly = false)
    @Override
    public String importData(List<SearchInfo> searchInfos) {
        if (StringUtils.isNull(searchInfos) || searchInfos.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SearchInfo searchInfo : searchInfos) {
            try {
                searchInfo.setStatus("0");
                super.save(searchInfo);
                successNum++;
            } catch (Exception e) {
                failureNum++;
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，");
        }
        return successMsg.toString();
    }

    @Override
    public SearchInfo findOne(SearchInfo searchInfo) {
        return mapper.findOne(searchInfo);
    }
}
